var gh = (function() {
  return {
    onload: function () {
      xia();
      chooseClass();
   }
  };

function chooseClass() {
    IELTSButton = document.querySelector('#IELTS');
    IELTSButton.onclick = IELTS;
    TOEFLButton = document.querySelector('#TOEFL');
    TOEFLButton.onclick = TOEFL;
    chrome.storage.local.get(['class'], function(result) {
        document.getElementById(result.class).style.backgroundColor="pink";
    });


    }

function IELTS() {
    chrome.storage.local.set({class:"IELTS"}, function () {
    });
    document.getElementById("IELTS").style.backgroundColor="pink";
    document.getElementById("TOEFL").style.backgroundColor="";

    document.getElementById("info").innerText="";
}
function TOEFL() {
        chrome.storage.local.set({class:"TOEFL"}, function () {
        });
    document.getElementById("TOEFL").style.backgroundColor="pink";
    document.getElementById("IELTS").style.backgroundColor="";
    document.getElementById("info").innerText="";
}

function xia() {
    chrome.storage.local.get(['class'], function(result) {

  qixia('GET', 'http://localhost/getWord/'+result.class,
      false, qi);
  setTimeout(setChinese,1000);
    });
}
function setChinese() {
    chrome.storage.local.get(['chinese'], function(result) {
        document.getElementById("chinese").innerText=result.chinese;
    });
}
function qixia(method, url, interactive, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open(method, url);
xhr.onload = requestComplete;
  xhr.send();
function requestComplete() {
  if ( ( this.status < 200 || this.status >=300 ) ) {
  } else {
    callback(null, this.status, this.response);
  }
}
}
function qi(error, status, response) {
  if (!error && status == 200) {
    var word_info = JSON.parse(response);
    document.getElementById("english").innerText=word_info.English;
    chrome.storage.local.set({chinese: word_info.Chinese}, function () {
      });
  }
}

})();




window.onload = gh.onload;